package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Course;
import com.eece496.Database.Enrollment;
import com.eece496.Database.Grades;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TASaveData;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.CourseInfo;
import com.eece496.Helpers.StudentInfo;
import com.eece496.Helpers.TAInfo;

/**
 * Servlet implementation class ProfModifyServlet
 */
public class ProfModifyServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfModifyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String ButtonClicked = request.getParameter("button");
		
		
		
		if (ButtonClicked.equals("Add Student")) 
		{	
			String courseNumber = request.getParameter("courseNumber");
			String sectionNumber = request.getParameter("sectionNumber");
			
			System.out.println(sectionNumber);
			
			System.out.println(firstName + " " + lastName);
			
			
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("firstname", firstName);
			request.setAttribute("lastname", lastName);
			request.getRequestDispatcher("/Professor/Professor-Add-Student.jsp")
					.forward(request, response);
		} 
		else if (ButtonClicked.equals("Remove Student")) {
			
			String String1 = request.getParameter("courseNumber");
			String courseNumber = String1.substring(0, 8);
			String sectionNumber = String1.substring(9, 12);
			String studentID = String1.substring(13, 21);
			int sID = Integer.parseInt(studentID);
			
			StudentGroups StudentGroupDB = new StudentGroups();
			StudentGroupDB.Setup(connection);
			
			Grades GradesDB = new Grades();
			GradesDB.Setup(connection);
			
			Enrollment EnrollmentDB = new Enrollment();
			EnrollmentDB.Setup(connection);
			
			TAduties TADutyDB = new TAduties();
			TADutyDB.Setup(connection);
			
			try {
				//Delete TA duties
				TADutyDB.DeleteStudentDuty(sID,courseNumber,sectionNumber);
				//Delete Student from Groups
				StudentGroupDB.DeleteStudentFromGroup(sID, courseNumber, sectionNumber);
				//Delete Student Grades
				GradesDB.DeleteStudentGrades(sID, courseNumber, sectionNumber);
				//Delete Student Enrollment
				EnrollmentDB.DeleteStudentEnrollment(sID, courseNumber, sectionNumber);
				
				TAEnrollment TAEnrollmentDB = new TAEnrollment();
				TAEnrollmentDB.Setup(connection);

				ArrayList<StudentInfo> sInfo = new ArrayList<StudentInfo>();
				ArrayList<TAInfo> tInfo = new ArrayList<TAInfo>();

				sInfo = EnrollmentDB.GetAllStudentsEnrolled(courseNumber,
						sectionNumber);
				tInfo = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber,
						sectionNumber);
				
				request.setAttribute("firstname", firstName);
				request.setAttribute("lastname", lastName);
				request.setAttribute("courseNumber", courseNumber);
				request.setAttribute("sectionNumber", sectionNumber);
				request.setAttribute("StudentInfo", sInfo);
				request.setAttribute("TAInfo", tInfo);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			request.getRequestDispatcher("/Professor/Professor-View-Class-List.jsp")
					.forward(request, response);
		}
		else if(ButtonClicked.equals("Add TA"))
		{
			String courseNumber = request.getParameter("courseNumber");
			String sectionNumber = request.getParameter("sectionNumber");
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("firstname", firstName);
			request.setAttribute("lastname", lastName);
			request.getRequestDispatcher("/Professor/Professor-Add-TA.jsp")
					.forward(request, response);
		}
		else if(ButtonClicked.equals("Remove TA"))
		{
			String String1 = request.getParameter("courseNumber");
			String courseNumber = String1.substring(0, 8);
			String sectionNumber = String1.substring(9, 12);
			String studentID = String1.substring(13, 21);
			int sID = Integer.parseInt(studentID);
			
			Enrollment EnrollmentDB = new Enrollment();
			EnrollmentDB.Setup(connection);
			
			try {
				//Delete their Saved Data
				TASaveData TADataDB = new TASaveData();
				TADataDB.Setup(connection);
				TADataDB.DeleteTAData(sID, courseNumber, sectionNumber);
				
				//Delete their Duties
				TAduties TADutyDB = new TAduties();
				TADutyDB.Setup(connection);
				TADutyDB.DeleteTADuty(sID, courseNumber, sectionNumber);
				
				//Delete from enrollment db
				TAEnrollment TAEnrollmentDB = new TAEnrollment();
				TAEnrollmentDB.Setup(connection);
				TAEnrollmentDB.DeleteStudentEnrollment(sID, courseNumber, sectionNumber);
				
				ArrayList<StudentInfo> sInfo = new ArrayList<StudentInfo>();
				ArrayList<TAInfo> tInfo = new ArrayList<TAInfo>();

				sInfo = EnrollmentDB.GetAllStudentsEnrolled(courseNumber,
						sectionNumber);
				tInfo = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber,
						sectionNumber);
				
				request.setAttribute("firstname", firstName);
				request.setAttribute("lastname", lastName);
				request.setAttribute("courseNumber", courseNumber);
				request.setAttribute("sectionNumber", sectionNumber);
				request.setAttribute("StudentInfo", sInfo);
				request.setAttribute("TAInfo", tInfo);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			request.getRequestDispatcher("/Professor/Professor-View-Class-List.jsp")
					.forward(request, response);
		}
	}
}